﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using DslModeling=global::Microsoft.VisualStudio.Modeling;
using DslDiagrams=global::Microsoft.VisualStudio.Modeling.Diagrams;
using DslValidation=global::Microsoft.VisualStudio.Modeling.Validation;

namespace Salamanca.Saml
{

    [DslValidation.ValidationState(DslValidation.ValidationState.Enabled)]
    partial class ActivityData
    {

        /// <summary>Ensures that data names are unique.</summary>
        [DslValidation.ValidationMethod(DslValidation.ValidationCategories.Menu | DslValidation.ValidationCategories.Open | DslValidation.ValidationCategories.Save)]
        private void ValidateAttributesNames(DslValidation.ValidationContext context)
        {
            IEnumerable<IGrouping<string, DataAttribute>> duplicates=Attributes
                .GroupBy<DataAttribute, string>(da => da.Name)
                .Where<IGrouping<string, DataAttribute>>(g => g.Count<DataAttribute>()>1);

            foreach (IGrouping<string, DataAttribute> group in duplicates)
                context.LogError(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.SR.DuplicateAttributeName,
                        group.Key
                    ),
                    "SAML02",
                    group.Cast<DslModeling.ModelElement>().ToArray<DslModeling.ModelElement>()
                );
        }

        /// <summary>Gets the <see cref="dataAttribute" /> with the specified name.</summary>
        /// <returns>The <see cref="dataAttribute" /> with the specified name.</returns>
        public DataAttribute this[string name]
        {
            get
            {
                return Attributes.FirstOrDefault<DataAttribute>(da => string.Compare(da.Name, name, false, CultureInfo.InvariantCulture)==0);
            }
        }
    }
}
